from pyecharts.charts import Line
from pyecharts import options as opts
from pyecharts.commons import utils

"""
pyecharts官网：
https://pyecharts.org/#/zh-cn/
"""
# 用JS画出填充的颜色
gradient_color_js = (
    "new echarts.graphic.LinearGradient(0, 0, 0, 1, "
    "[{offset: 0, color: '#c86589'}, {offset: 1, color: '#06a7ff'}], false)"
)
background_color = "#F8F8FF"


# 变量date, H_Tem, L_Tem, day
def DrawChart(H_Tem, L_Tem, date, Tem, UpdateTime, City, MarkLine, Weekday):
    # 图表对象
    Chart = Line(init_opts=opts.InitOpts(bg_color=background_color,
                                         # 设置图幅大小
                                         width="1000px", height="550px"))
    # 增加自变量坐标
    Chart.add_xaxis(xaxis_data=date)

    # 增加一条因变量数据
    Chart.add_yaxis('最高温度',
                    y_axis=H_Tem,
                    symbol="emptyCircle",

                    # 显示线上的圆点
                    is_symbol_show=True,
                    # True为在曲线上忽略（不连接）空数据点
                    is_connect_nones=True,
                    # 最高温的包络线为虚线
                    linestyle_opts=opts.LineStyleOpts(type_='dashed'),
                    # 平滑过渡
                    is_smooth=True,
                    # 显示线上的数值
                    label_opts=opts.LabelOpts(is_show=True, color="#FF0000"),
                    # 此处为堆叠区域，线的16进制色号为"#5A6B8F"，区域为"#8995AF"
                    areastyle_opts=opts.AreaStyleOpts(opacity=0.3, color=utils.JsCode(gradient_color_js)),
                    # areastyle_opts=opts.AreaStyleOpts(color=background_color. opacity=1)
                    )

    Chart.add_yaxis('实时温度',
                    y_axis=Tem,
                    symbol="emptyCircle",
                    # 显示线上的圆点
                    is_symbol_show=True,
                    # 平滑过渡
                    is_smooth=True,
                    # True为在曲线上忽略（不连接）空白数据点
                    is_connect_nones=True,
                    # 显示线上的数值
                    label_opts=opts.LabelOpts(is_show=True),
                    # 标记点的属性用字典格式传入itemstyle_opts
                    markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max",
                                                                               name="最大值",
                                                                               itemstyle_opts={'opacity': '0.8',
                                                                                               'color': '#FF0000'}),
                                                            opts.MarkPointItem(type_="min",
                                                                               name="最小值",
                                                                               itemstyle_opts={'opacity': '0.8',
                                                                                               'color': '#0000FF'})
                                                            ]),
                    linestyle_opts=opts.LineStyleOpts(curve=0.5,
                                                      opacity=1,
                                                      width=2,
                                                      color={
                                                          'type': 'linear',
                                                          'x': 0,
                                                          'y': 0,
                                                          'x2': 0,
                                                          'y2': 1,
                                                          'colorStops': [{'offset': 0, 'color': 'red'},
                                                                         {'offset': 1, 'color': 'blue'}]}))
    # 增加一条因变量数据
    Chart.add_yaxis('最低温度',
                    y_axis=L_Tem,
                    symbol="emptyCircle",
                    # 显示线上的圆点
                    is_symbol_show=True,
                    # True为在曲线上忽略（不连接）空数据点
                    is_connect_nones=True,
                    # 平滑过渡
                    is_smooth=True,
                    # 显示线上的数值
                    label_opts=opts.LabelOpts(is_show=True, color="#0000FF"),
                    # 此处为堆叠区域，线的16进制色号为"#5A6B8F"，区域为"#8995AF"
                    areastyle_opts=opts.AreaStyleOpts(opacity=0.7, color=background_color),
                    # areastyle_opts=opts.AreaStyleOpts(color=background_color, opacity=1)
                    # 最高温的包络线为虚线
                    linestyle_opts=opts.LineStyleOpts(type_='dashed',
                                                      curve=0.5,
                                                      opacity=1,
                                                      width=1,
                                                      color="#483D8B")
                    )
    '''
    # 增加一条因变量作为X轴分割线的提示
    Chart.add_yaxis('日期',
                    y_axis=Weekday,
                    symbol="emptyCircle",
                    # 显示线上的圆点
                    is_symbol_show=True,
                    # True为在曲线上忽略（不连接）空数据点
                    is_connect_nones=True,
                    # 平滑过渡
                    is_smooth=True,
                    # 显示线上的数值
                    label_opts=opts.LabelOpts(is_show=True, color="#0000FF"),
                    # 此处为堆叠区域，线的16进制色号为"#5A6B8F"，区域为"#8995AF"
                    areastyle_opts=opts.AreaStyleOpts(opacity=0.7, color=background_color),
                    # areastyle_opts=opts.AreaStyleOpts(color=background_color, opacity=1)
                    # 最高温的包络线为虚线
                    linestyle_opts=opts.LineStyleOpts(type_='dashed',
                                                      curve=0.5,
                                                      opacity=1,
                                                      width=1,
                                                      color="#483D8B")
                    )
                    '''
    # 设置图标名称
    Chart.set_global_opts(tooltip_opts=opts.TooltipOpts(is_show=True),
                          title_opts=opts.TitleOpts(title=City, subtitle="UpdateTime:" + UpdateTime),
                          yaxis_opts=opts.AxisOpts(type_="value",
                                                   # True为隐藏线上的圆点及数值
                                                   # axistick_opts=opts.AxisTickOpts(is_show=True),
                                                   # True为显示平行于X轴的分割线
                                                   splitline_opts=opts.SplitLineOpts(is_show=True)),
                          # True为X轴的系列名显示在两刻度的中间，False显示在每一刻度之上
                          xaxis_opts=opts.AxisOpts(type_="category",
                                                   # True为显示平行于y轴的分割线
                                                   splitline_opts=opts.SplitLineOpts(is_show=False),
                                                   boundary_gap=False),
                          # X轴缩放
                          # datazoom_opts=opts.DataZoomOpts()
                          )
    '''
    # 设置X轴分区色块
    Chart.set_series_opts(
        markarea_opts=opts.MarkAreaOpts(
            # 边线
            data=[opts.MarkAreaItem(
                name="星期一",
                x=("01-19 08:00", "01-20 08:00"),
                itemstyle_opts={"borderColor": "write",
                                "borderWidth": 1})],
            # TODO:去除交互
            # 中心
            itemstyle_opts=opts.ItemStyleOpts(border_type='dotted',
                                              color='transparent')))

    '''

    # 设置X轴分区划线
    Chart.set_series_opts(
        markline_opts=opts.MarkLineOpts(
            # 不响应鼠标
            is_silent=True,
            # 取消markline终点的箭头
            symbol='none',
            # 取消markline起点的箭头
            #用for循环获取Markline中的数据
            data=[opts.MarkLineItem(x=MarkLine[i], symbol='none') for i in range(len(MarkLine))
                  ],
            # 不显示文字提示
            label_opts=opts.LabelOpts(is_show=False),
            linestyle_opts=opts.LineStyleOpts(width=2,
                                              opacity=1,
                                              type_='dotted',
                                              color={
                                                  'type': 'linear',
                                                  'x': 0,
                                                  'y': 0,
                                                  'x2': 0,
                                                  'y2': 1,
                                                  'colorStops': [{'offset': 0, 'color': '#06EB00'},
                                                                 {'offset': 1, 'color': background_color}]})))

    return Chart


def Generate(dictinfo):
    HoursDay = dictinfo['HoursDate']
    UpdateTime = dictinfo['UpdateTime']
    City = dictinfo['City']

    HighlyTemperature = dictinfo['MaximumTemperature']
    RealTimeTemperature = dictinfo['HoursTemperature']
    LowlyTemperature = dictinfo['MinimumTemperature']

    MarkLine = dictinfo['MarkLine']
    Weekday = dictinfo['Weekday']
    ChartObject = DrawChart(HighlyTemperature, LowlyTemperature, HoursDay, RealTimeTemperature, UpdateTime,
                            City, MarkLine, Weekday)
    ChartObject.render("WeatherReport.html")
    print(Weekday)
    return


if __name__ == '__main__':
    Dictinfo = {'UpdateTime': '2021-01-30 11:46:17', 'City': '佛山',
                'day': ['星期六', '星期日', '星期一', '星期二', '星期三', '星期四', '星期五'],
                'date': ['2021-01-30', '2021-01-31', '2021-02-01', '2021-02-02', '2021-02-03', '2021-02-04',
                         '2021-02-05'],
                'HoursDate': ['01-30 08:00', '01-30 11:00', '01-30 14:00', '01-30 17:00', '01-30 20:00', '01-30 23:00',
                              '01-31 02:00', '01-31 05:00', '01-31 08:00', '01-31 11:00', '01-31 14:00', '01-31 17:00',
                              '01-31 20:00', '01-31 23:00', '02-01 02:00', '02-01 05:00', '02-01 08:00', '02-01 11:00',
                              '02-01 14:00', '02-01 17:00', '02-01 20:00', '02-01 23:00', '02-02 02:00', '02-02 05:00',
                              '02-02 08:00', '02-02 11:00', '02-02 14:00', '02-02 17:00', '02-02 20:00', '02-02 23:00',
                              '02-03 02:00', '02-03 05:00', '02-03 08:00', '02-03 11:00', '02-03 14:00', '02-03 17:00',
                              '02-03 20:00', '02-03 23:00', '02-04 02:00', '02-04 05:00', '02-04 08:00', '02-04 11:00',
                              '02-04 14:00', '02-04 17:00', '02-04 20:00', '02-04 23:00', '02-05 02:00', '02-05 05:00',
                              '02-05 08:00', '02-05 11:00', '02-05 14:00', '02-05 17:00', '02-05 20:00', '02-05 23:00',
                              '02-06 02:00'],
                'HoursWeather': ['晴', '晴', '晴', '晴', '晴', '晴', '晴', '晴', '晴', '晴', '晴', '晴', '晴', '晴', '晴', '晴', '晴',
                                 '晴', '晴', '晴', '晴', '阴', '阴', '阴', '阴', '', '多云', '', '晴', '', '多云', '', '多云', '',
                                 '多云', '', '多云', '', '晴', '', '多云', '', '晴', '', '晴', '', '晴', '', '晴', '', '晴', '',
                                 '晴', '', '晴'],
                'HoursTemperature': ['13', '18', '21', '22', '14', '13', '12', '13', '14', '20', '23', '24', '18', '15',
                                     '13', '13', '15', '20', '24', '25', '22', '16', '16', '15', '15', '', '21', '',
                                     '18', '', '15', '', '12', '', '21', '', '17', '', '12', '', '13', '', '22', '',
                                     '18', '', '14', '', '14', '', '28', '', '24', '', '19'],
                'HoursWin': ['无持续风向', '无持续风向', '无持续风向', '无持续风向', '无持续风向', '无持续风向', '无持续风向', '无持续风向', '无持续风向', '无持续风向',
                             '无持续风向', '无持续风向', '无持续风向', '无持续风向', '无持续风向', '无持续风向', '无持续风向', '无持续风向', '无持续风向', '无持续风向',
                             '无持续风向', '无持续风向', '无持续风向', '无持续风向', '无持续风向', '', '无持续风向', '', '无持续风向', '', '无持续风向', '',
                             '无持续风向', '', '无持续风向', '', '无持续风向', '', '无持续风向', '', '无持续风向', '', '无持续风向', '', '无持续风向', '',
                             '无持续风向', '', '无持续风向', '', '无持续风向', '', '无持续风向', '', '无持续风向'],
                'HoursWinSpeed': ['<3级', '<3级', '<3级', '<3级', '<3级', '<3级', '<3级', '<3级', '<3级', '<3级', '<3级', '<3级',
                                  '<3级', '<3级', '<3级', '<3级', '<3级', '<3级', '<3级', '<3级', '<3级', '<3级', '<3级', '<3级',
                                  '<3级', '', '<3级', '', '<3级', '', '<3级', '', '<3级', '', '<3级', '', '<3级', '', '<3级',
                                  '', '<3级', '', '<3级', '', '<3级', '', '<3级', '', '<3级', '', '<3级', '', '<3级', '',
                                  '<3级'],
                'MinimumTemperature': ['', '', '', '', '', '', '', '', '12', '', '', '', '', '', '', '', '13', '', '',
                                       '', '', '', '', '', '15', '', '', '', '', '', '', '', '12', '', '', '', '', '',
                                       '', '', '12', '', '', '', '', '', '', '', '14', '', '', '', '', '', ''],
                'MaximumTemperature': ['', '', '22', '', '', '', '', '', '', '', '24', '', '', '', '', '', '', '', '25',
                                       '', '', '', '', '', '', '', '23', '', '', '', '', '', '', '', '22', '', '', '',
                                       '', '', '', '', '23', '', '', '', '', '', '', '', '28', '', '', '', ''],
                'MarkLine': ['01-31 02:00', '02-01 02:00', '02-02 02:00', '02-03 02:00', '02-04 02:00', '02-05 02:00',
                             '02-06 02:00'],
                'Weekday': ['周三 01-31', '周四 02-01', '周五 02-02', '周六 02-03', '周日 02-04', '周一 02-05', '周二 02-06']}

    Generate(Dictinfo)
"""
#增加平均线
Chart.add_yaxis('最高温度', H_Tem, is_smooth=True,
                    markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]))
#增加某节点的标记
Chart.add_yaxis('最低温度', L_Tem, is_smooth=True,
                    markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(name="自定义标记点",
                                                                               coord=[cate[2], LowlyTemperature[2]],
                                                                               value=LowlyTemperature[2])]))
"""
